package initialize

import (
	"a_plus_station/global"
	"a_plus_station/models"
	"a_plus_station/tables"
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"go.uber.org/zap"
)

func AfterCreate() {
	if global.EsIndexExists == true {
		batchSize := 1500
		offset := 0
		ctx := context.Background()

		loop := true
		for loop {
			var goods []tables.Goods
			result := global.DB.Limit(batchSize).Offset(offset).Preload("GoodsDesc").Find(&goods)
			if result.Error != nil {
				zap.S().Infof("Error reading data: %v", result.Error.Error())
			}

			bulkRequest := global.EsClient.Bulk()
			for _, product := range goods {
				fmt.Println("ID: %s, name: %s, categoryID: %s", product.GoodsID, product.Name, product.CategoryID)

				var faveNum int64
				result := global.DB.Model(&tables.Collect{}).Where(&tables.Collect{GoodsID: product.GoodsID}).Count(&faveNum)
				if result.Error != nil {
					zap.S().Infof("Error reading data: %v", result.Error.Error())
				}

				createdAt := product.GoodsDesc.CreatedAt.Format(tables.FormatTime)
				updatedAt := product.GoodsDesc.CreatedAt.Format(tables.FormatTime)

				out_product := models.EsGoods{
					GoodsID:       product.GoodsID,
					CategoryID:    product.CategoryID,
					Name:          fmt.Sprintf("Sample Product %s", product.Name),
					GoodsDesc:     product.Desc,
					Click:         product.Click,
					Score:         product.Score,
					ShopMinPrice:  product.OutMinPrice,
					ShopMaxPrice:  product.OutMaxPrice,
					PackingMode:   product.PackingMode,
					LeadTime:      product.LeadTime,
					DeliveryTime:  product.DeliveryTime,
					MainPicture:   product.MainPicture,
					IsNew:         product.IsNew,
					ISHot:         product.IsHot,
					KeyWords:      string(product.KeyWords),
					FaveNum:       int32(faveNum),
					Color:         product.GoodsDesc.Color,
					ProductLength: product.GoodsDesc.ProductLength,
					ProductWidth:  product.GoodsDesc.ProductWidth,
					ProductHeight: product.GoodsDesc.ProductHeight,
					ProductWeight: product.GoodsDesc.ProductWeight,
					Material:      product.GoodsDesc.Material,
					Image:         product.GoodsDesc.Image,
					Qty1:          product.GoodsDesc.Qty1,
					Price1:        product.GoodsDesc.Price1,
					Qty2:          product.GoodsDesc.Qty2,
					Price2:        product.GoodsDesc.Price2,
					Qty3:          product.GoodsDesc.Qty3,
					Price3:        product.GoodsDesc.Price3,
					Qty4:          product.GoodsDesc.Qty4,
					Price4:        product.GoodsDesc.Price4,
					Qty5:          product.GoodsDesc.Qty5,
					Price5:        product.GoodsDesc.Price5,
					Qty6:          product.GoodsDesc.Qty6,
					Price6:        product.GoodsDesc.Price6,
					Qty7:          product.GoodsDesc.Qty7,
					Price7:        product.GoodsDesc.Price7,
					Qty8:          product.GoodsDesc.Qty8,
					Price8:        product.GoodsDesc.Price8,
					CreatedAt:     createdAt,
					UpdatedAt:     updatedAt,
				}
				req := elastic.NewBulkIndexRequest().Index(models.EsGoods{}.GetIndexName()).Id(out_product.GoodsID).Doc(out_product)
				bulkRequest = bulkRequest.Add(req)
			}

			bulkResponse, err := bulkRequest.Do(ctx)
			if err != nil {
				zap.S().Infof("Error executing bulk request: %v", err.Error())
			}
			zap.S().Infof("Bulk request completed with %d items \n", len(bulkResponse.Items))

			if len(goods) != 1500 {
				loop = false
				break
			}

			offset += batchSize
		}
	}

	////单条插入
	//ctx := context.Background()
	//_, err := global.EsClient.Index().Index(models.EsGoods{}.GetIndexName()).Id(fmt.Sprintf("%s", prodyct.ID)).Do(ctx)
	//if err != nil {
	//	zap.S()
	//}
}
